package middleware

import (
	"common"
	"context"
	"google.golang.org/grpc"
	"strings"
)

var _methods = []string{
	"favourite",
	"index",
	"isfavourite",
}

// UserAuthCheck 检查用户授权
func UserAuthCheck(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handle grpc.UnaryHandler) (resp interface{}, err error) {
	fullMethod := strings.Split(info.FullMethod, "/")
	method := fullMethod[len(fullMethod)-1]
	if common.InArray(strings.ToLower(method), _methods) {
		_, err = common.GetUserByGrpc(ctx)
		if err != nil {
			return nil, err
		}
	}
	return handle(ctx, req)
}
